use crate::{bootstrap::AppState, middleware::jwt::admin::Claims, router::resp::RespVO};
use axum::{
    body::Body,
    extract::{Path, State},
    http::Response,
    response::IntoResponse,
};
use redis::AsyncCommands;
use sky_common::constant;

pub async fn set_shop_status(
    _claim: Claims,
    State(mut state): State<AppState>,
    Path(status): Path<i32>,
) -> Response<Body> {
    // 根据传入的 status 值设置状态信息
    let info = if status == 1 {
        constant::shop::SHOP_STATUS_1
    } else {
        constant::shop::SHOP_STATUS_0
    };
    let status_info = format!("设置店铺的营业状态为：{}", info);

    tracing::info!(status_info);

    match state
        .redis
        .set::<String, i32, ()>(constant::shop::SHOP_STATUS_KEY.to_owned(), status) // 使用 &str 作为键
        .await
    {
        Ok(_) => RespVO::<i32>::success(status_info.to_owned(), Some(status)).into_response(),
        Err(e) => RespVO::<String>::error("设置店铺状态失败".to_owned(), Some(e.to_string()))
            .into_response(),
    }
}

pub async fn get_shop_status(State(mut state): State<AppState>) -> Response<Body> {
    match state
        .redis
        .get::<String, i32>(constant::shop::SHOP_STATUS_KEY.to_owned()) // 获取 Redis 中的状态值
        .await
    {
        Ok(status) => {
            let info = if status == 1 {
                constant::shop::SHOP_STATUS_1
            } else {
                constant::shop::SHOP_STATUS_0
            };
            let status_info = format!("获取到店铺的营业状态为：{}", info);

            tracing::info!(status_info);

            RespVO::<i32>::success(status_info.to_owned(), Some(status)).into_response()
        }
        Err(e) => RespVO::<String>::error("获取店铺状态失败".to_owned(), Some(e.to_string()))
            .into_response(),
    }
}
